﻿using SqlSugar;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WCS.Utils
{
    public class DBContext
    {
        public static string connString = System.Configuration.ConfigurationManager.AppSettings["SQLConn"].ToString();

        public static SqlSugarClient GetInstance()
        {
            return new SqlSugarClient(
                new ConnectionConfig()
                {
                    //ConnectionString = "server=.;uid=sa;pwd=@jhl85661501;database=SqlSugar4XTest",
                    ConnectionString = connString,
                    DbType = DbType.SqlServer,//设置数据库类型，非配置
                    IsAutoCloseConnection = true,//自动释放数据务，如果存在事务，在事务结束后释放
                    InitKeyType = InitKeyType.Attribute //从实体特性中读取主键自增列信息
                });


            //Client.CodeFirst.InitTables(typeof(Dept), typeof(Person), typeof(Employee));
            //Client.Aop.OnLogExecuting = (sql, paramters) =>
            //{
            //    Console.WriteLine(sql);
            //};
        }
    }

    public class SqlSugarDapper
    {
        public SqlSugarClient DB
        {
            get
            {
                return new SqlSugarClient(new ConnectionConfig()
                {
                    ConnectionString = ConfigurationManager.AppSettings["SQLConn"],
                    DbType = DbType.SqlServer,//设置数据库类型
                    IsAutoCloseConnection = true,//自动释放数据务，如果存在事务，在事务结束后释放
                    InitKeyType = InitKeyType.Attribute //从实体特性中读取主键自增列信息
                });
            }
        }

        public static SqlSugarDapper GetDbContext()
        {
            return new SqlSugarDapper();
        }

        public static SqlSugarClient GetDbContextTran()
        {
            return new SqlSugarDapper().DB;
        }
    }
}
